package cn.chinasyq.photoquan.wxapi;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import cn.chinasyq.photoquan.AppConstant;
import cn.chinasyq.photoquan.R;


public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
	
	private static final String TAG = WXPayEntryActivity.class.getCanonicalName();
	
    private IWXAPI api;

	public static boolean weixinPaySuccessed;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_result);
        
    	api = WXAPIFactory.createWXAPI(this, AppConstant.APP_ID);
        api.handleIntent(getIntent(), this);
    }

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
        api.handleIntent(intent, this);
	}

	@Override
	public void onReq(BaseReq req) {
//		ToastUtil.show("7889345793579347598========");
		
	}

	@Override
	public void onResp(BaseResp resp) {
		Log.e(TAG, "onPayFinish, errCode = " + resp.errCode);
		//第三方通过微信支付
		if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
			if (resp.errCode == BaseResp.ErrCode.ERR_OK) {
				weixinPaySuccessed = true;
				finish();
			}else if(resp.errCode == BaseResp.ErrCode.ERR_USER_CANCEL){
				finish();
			}else if(resp.errCode == BaseResp.ErrCode.ERR_AUTH_DENIED){
//				ToastUtil.showTips(R.drawable.tips_smile, "认证被否决");
				finish();
			}else if(resp.errCode == BaseResp.ErrCode.ERR_SENT_FAILED){
//				ToastUtil.showTips(R.drawable.tips_smile, "发送失败");
				finish();
			}else if(resp.errCode == BaseResp.ErrCode.ERR_UNSUPPORT){
//				ToastUtil.showTips(R.drawable.tips_smile, "不支持错误");
				finish();
			}else if(resp.errCode == BaseResp.ErrCode.ERR_COMM){
//				ToastUtil.showTips(R.drawable.tips_smile, "一般错误");
				finish();
			}else {
				((TextView)findViewById(R.id.textView)).setText(resp.errCode+","+resp.errStr);
				findViewById(R.id.layout).setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						finish();
					}
				});
			}
		}
	}
}